/**
*****************************************************************************
 * DESC ：. 
 * Author : Junwei Zhang 
 * CREATEDATE ：2012-7-20 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-7-20		Created.
 ****************************************************************************
**/
package com.me.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

/**
 * 
 * <br/> 上午9:04:15 2012-7-20
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
public class JTalkChatRoomCreater extends JDialog implements JTalkClientCommandInterface{

	private JTalkClientFront parent = null;
	
	private JPanel jPanelImage = null;
	private JPanel jPanelCreateChatRoom = null;
	private JLabel jLabelImage = null;
	
	private ClassLoader classLoader = null;

	private JLabel jLabelCreateChatRoom;

	private JLabel jLabelRoomName;

	private JLabel jLabelUsers;

	private JTextField jTextFieldRoomName;

	private JTextField jTextFieldUsers;

	private JButton jButtonCreate;

	private JLabel jLabelStatus;
	
	public JTalkChatRoomCreater(JTalkClientFront parent, boolean modal) {
		// TODO Auto-generated constructor stub
		super(parent, modal);
		this.parent = parent;
		this.initComponents();
		this.setTitle("Create a ChatRoom");
		this.parent.theClient.registerForCommandResponse(this);
	}

	private void initComponents() {
		classLoader = JTalkChatRoomCreater.class.getClassLoader();
		jPanelImage = new JPanel();
		jLabelImage = new JLabel();
		jLabelImage.setIcon(new ImageIcon(classLoader.getResource("com/jtalker/rs/icons/new_chatroom.jpg")));
		jLabelImage.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		jPanelImage.add(jLabelImage);
		this.getContentPane().add(jPanelImage, BorderLayout.WEST);
		
		jPanelCreateChatRoom = new JPanel();
		jPanelCreateChatRoom.setLayout(null);
		jPanelCreateChatRoom.setPreferredSize(new Dimension(300, 195));
		
		jLabelCreateChatRoom = new JLabel();
		jLabelCreateChatRoom.setFont(new Font("Dialog", 1, 18));
		jLabelCreateChatRoom.setHorizontalAlignment(SwingConstants.CENTER);
		jLabelCreateChatRoom.setText("Create ChatRoom");
		jLabelCreateChatRoom.setBounds(40, 15, 220, 25);
		jPanelCreateChatRoom.add(jLabelCreateChatRoom);
		
		jLabelRoomName = new JLabel();
		jLabelRoomName.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelRoomName.setText("Room Name:");
		jLabelRoomName.setBounds(10, 60, 110, 18);
		jPanelCreateChatRoom.add(jLabelRoomName);
		
		jLabelUsers = new JLabel();
		jLabelUsers.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelUsers.setText("Users:");
		jLabelUsers.setBounds(10, 90, 110, 18);
		jPanelCreateChatRoom.add(jLabelUsers);
		
		jTextFieldRoomName = new JTextField();
		jTextFieldRoomName.setText(this.parent.theClient.userName + "-room");
		jTextFieldRoomName.setToolTipText("Name for the chatroom");
		jTextFieldRoomName.setBounds(130, 60, 150, 22);
		jPanelCreateChatRoom.add(jTextFieldRoomName);
		
		jTextFieldUsers = new JTextField();
		jTextFieldUsers.setText(this.parent.theClient.userName + ",");
		jTextFieldUsers.setToolTipText("Allowed users, separated by ','");
		jTextFieldUsers.setBounds(130, 90, 150, 22);
		jPanelCreateChatRoom.add(jTextFieldUsers);
		
		jButtonCreate = new JButton();
		jButtonCreate.setText("Create");
		jButtonCreate.setToolTipText("Create a chatroom");
		jButtonCreate.setBounds(190, 130, 88, 28);
		jButtonCreate.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				createChatRoom(e);
			}
		});
		jPanelCreateChatRoom.add(jButtonCreate);
		
		jLabelStatus = new JLabel();
		jLabelStatus.setHorizontalAlignment(SwingConstants.CENTER);
		jLabelStatus.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		jLabelStatus.setBounds(10, 170, 270, 20);
		jPanelCreateChatRoom.add(jLabelStatus);
		
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				closeDialog(e);
			}
		});
		this.getContentPane().add(jPanelCreateChatRoom, BorderLayout.CENTER);
		this.pack();
		
	}
	
	/**
	 * 创建聊天室.
	 * @param e
	 */
	private void createChatRoom(ActionEvent e) {
		// TODO Auto-generated method stub
		this.jLabelStatus.setText("Checking inputs...");
		String roomName = this.jTextFieldRoomName.getText();
		String roomUsers = this.jTextFieldUsers.getText();
		if(this.parent.isConnected){
			// System.out.println("-----this.parent.isConnected-----" + this.parent.isConnected);
			this.parent.theClient.sendMessage("std::createChatRoom(\"" + roomName + "\", \"" + roomUsers + "\")", null
					, "command", null, "usercommand11");
		}
	}
	
	/**
	 * 关闭对话框.
	 * @param e
	 */
	private void closeDialog(WindowEvent e) {
		this.setVisible(false);
		this.dispose();
	}

	@Override
	public boolean setCommandAnswer(String messageID, String sender, String content) {
		if("usercommand11".equals(messageID)){
			this.jLabelStatus.setText(content);
		}
		return true;
	}

}
